#include "GridAgent.h"
#include "Track.h"
#include "FollowSearchPathState.h"

GridAgent::GridAgent(tank_t* tank)
{
	this->tank = tank;

	//getTrack();
	reverse = false;
	pos = 0;
	//currentState = new FollowSearchPathState(World::instance()->getGraph(), Vector2(tank->pos[0], tank->pos[1]), currentTrack[0], false);	
	currentState = 0;
}


GridAgent::~GridAgent(void)
{
}

Command GridAgent::GetCommand(){
	if (currentState)
		return currentState->GetCommand(this);

	return Command(this->tank->index, 0, 0, false);
}

tank_t* GridAgent::getTank(){
	return tank;
}

bool GridAgent::getTrack(){
	
	Track* t = World::instance()->getTrack();
	
	if(t->getNextTrack(&currentTrack)){
		pos = currentTrack.size()-1;
		currentState = new FollowSearchPathState(World::instance()->getGraph(), Vector2(tank->pos[0], tank->pos[1]), currentTrack[0], false);
	}

	return true;
}

void GridAgent::setState(AgentState* newState) {
	if (currentState) {
		currentState->exit(this);
		delete currentState;
	}

	currentState = newState;

	if (currentState)
		currentState->enter(this);
}
